<?php
/**
* This page is used to create a new page.
*     Redirects to template.php if this is for the homepage.
*
* @package folio
* @param string $page The page title for the new page.
* @param string $user The user/community to whom this page belongs.
**/
    define("context", "folio_page_create");

    require_once("../../../includes.php");
    require_once("../view/NewPage.php");

    require_once("../model/page_template.php");
    require_once("../model/user.php");
    require_once("../redirect.php");

    global $CFG;
    global $metatags;

    $username = folio_clean(required_param('user'));
    $url = url;
    $title = __gettext('Create a new page');

    // Load user.
    $user = folio_user::SelectWhereUsername( $username );
    if ( !$user )
        trigger_error('Invalid username ' . $username . ' passed to new.php.', E_USER_ERROR);

    // set the current user.
    global $page_owner;
    $page_owner = $user->ident;

    // Grab homepage.
    $homepage = folio_page::SelectHomepage($user);
    if( $homepage === false ) {
        // Need to create the homepage -- this should be done thru /template.php.
        //      Redirect to the proper location.
        header("Location: {$url}{$username}/page/home/template");
        exit();
    }

    // Set pagename and parent.
    if ( isset($_GET['page']) ) {
        // Adding a page with a known title but unknown parent.
        //    Happens when clicking on a link in a page to a new page.

        $page_title = folio_page::title_decode(required_param('page'));
        $parentpage = $homepage;

        // Attempt to load the page, and redirect if it exists.
        $page = folio_page::SelectWhereTitleAndUsername($page_title, $username);
        if ( !!$page ) {
            // Existing page.  Automatically re-direct to the page itself.
            header("Location: {$url}{$username}/page/" . folio_page::title_encode($page->title) );
            exit();
        }

    } elseif ( isset( $_GET['parentpage']) ) {
        // Adding a page with an unknown title, but known parent.
        //     Happens when clicking on the 'add a new page under this one' link.

        $page_title = __gettext('New page title');

        // Attempt to load the parent page.
        $name = folio_page::title_decode(required_param('parentpage'));

        $parentpage = folio_page::SelectWhereTitleAndUsername($name, $username);

        if( $parentpage == false )
            trigger_error('Invalid parentpage name "' . $name . '" for user "' .
                          $username . '" passed to new.php', E_USER_ERROR);

    } else {
        trigger_error('Invalid parameters passed to new.php, could not locate page or parentpage variables', E_USER_ERROR);
    }


    if ( !folio_page::InsertPermission( $user ) ) {
        // This user doesn't have the rights to create this page.

        // Homepage or normal page?
        if( $page_title == $FOLIO_CFG->page_defaulthomepage ) {
            // Looking at the homepage.  Nice error message.
            $body = __gettext('This user has not created a page named "' .
                              $FOLIO_CFG->page_defaulthomepage . '"');
        } else {
            // Looking at another page.  Not as nice error message.
            $body = __gettext('You do not have permission to create this page');
        }

    } else {
        // Create the controls for the new page.
        $form = new folio_NewPage( $user, $homepage, $parentpage, $page_title );
        $body = $form->toString();
    }

    header("Cache-control: private");
    $body = templates_draw(array(
                    'context' => 'contentholder',
                    'title' => $title,
                    'body' => $body
                ));
    echo templates_page_draw(array(
                    $title, $body
                ));

?>
